<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/3/3
 * Time: 17:35
 */

namespace app\admin\controller;

use Common\Oss;
use think\Config;
use think\Controller;
use think\Db;
use think\Request;

class Sundry extends Common
{

    /**
     * 上传轮播图
     * @return \think\response\Json
     */
    public function uploadImage()
    {
        $data = Request::instance()->post();
        if(array_key_exists('id' , $data) && !empty($data['id']))
        {
            if(array_key_exists('status' , $data) && !empty($data['status']))
            {
                $file = $_FILES;
                if($file)
                {
                    $ossClient = new Oss();
                    $files     = $ossClient->uploadFile('banner', 'image');
                    $save_data['image'] = join(",",$files);
                }
                $save_data['status'] = $data['status'];
                $status = Db::name('carousel')->where('id',$data['id'])->update($save_data);
                if($status)
                {
                    Db::name('carousel')->whereNotIn('id' , $data['id'])->update(['status' => 2]);
                }
                if($status)
                {
                    $result = Config::get('code.success');
                }
                else
                {
                    $result = Config::get('code.error');
                }
            }
            else
            {
                $result = Config::get('code.mistake');
            }
        }
        else
        {
            if(array_key_exists('status' , $data))
            {
                $file = $_FILES;
                if($file)
                {
                    $ossClient = new Oss();
                    $files     = $ossClient->uploadFile('test', 'image');
                    if($files)
                    {

                        $ins_data['image'] = join(",",$files);
                        $ins_data['status'] = $data['status'];
                        $ins_data['create_time'] = date('Y-m-d H:i:s');
                        $id = Db::name('carousel')->insertGetId($ins_data);
                        if($data['status'] == 1)
                        {
                            Db::name('carousel')->whereNotIn('id',$id)->update(['status' => 2]);
                        }
                        if($id)
                        {
                            $result = Config::get('code.success');
                        }
                        else
                        {
                            $result = Config::get('code.error');
                        }
                    }
                    else
                    {
                        $result = Config::get('code.error');
                    }
                }
                else
                {
                    $result = Config::get('code.mistake');
                }

            }
            else
            {
                $result = Config::get('code.mistake');
            }

        }
        return json($result);
    }


    /**
     * 获取轮播图列表数据
     * @return \think\response\Json
     */
    public function getCarouselList()
    {
        $post = Request::instance()->post();
        $page = $post['pageNumber'] ? $post['pageNumber'] : 1;
        $limit = $post['pageSize'] ? $post['pageSize'] : 10;
        $limit1= ($page - 1 ) * $limit . ',' . $limit;
        $info = Db::name('carousel')->order('create_time desc')->limit($limit1)->select();
        $count = Db::name('carousel')->count();
        if($info)
        {
            foreach ($info as $key => $value)
            {
                $info[$key]['image'] = explode(',',$value['image']);
                if($value['status'] == 1)
                {
                    $info[$key]['status'] = '显示';
                }
                else
                {
                    $info[$key]['status'] = '隐藏';
                }
            }
            $result = Config::get('code.success');
            $result['data'] = $info;
            $result['count'] = $count;
        }
        else
        {
            $result = Config::get('code.null');
            $result['data'] = [];
            $result['count'] = 0;
        }
        return json($result);
    }

    /**
     * 删除轮播图
     * @return \think\response\Json
     */
    public function delCarousel()
    {
        $id = input('post.id');
        if($id)
        {
            $status = Db::name('carousel')->where('id' , $id)->delete();
            if($status)
            {
                $result = Config::get('code.success');
            }
            else
            {
                $result = Config::get('code.error');
            }
        }
        else
        {
            $result = Config::get('code.mistake');
        }
        return json($result);
    }

    /**
     * 获取轮播图详情
     * @return \think\response\Json
     */
    public function getCarouselInfo()
    {
        $id = input('post.id');
        if($id)
        {
            $info = Db::name('carousel')->where('id',$id)->find();
            if($info)
            {
                $info['image'] = explode(',',$info['image']);
                $result = Config::get('code.success');
                $result['data'] = $info;
            }
            else
            {
                $result = Config::get('code.null');
            }
        }
        else
        {
            $result = Config::get('code.mistake');
        }
        return json($result);
    }

    /**
     * 意见反馈
     * @return \think\response\Json
     */
    public function getSuggestionList()
    {
        $post = Request::instance()->post();
        $page = $post['pageNumber'] ? $post['pageNumber'] : 1;
        $limit = $post['pageSize'] ? $post['pageSize'] : 10;
        $limit1= ($page - 1 ) * $limit . ',' . $limit;
        $where = [];
        if(array_key_exists('tel' , $post))
        {
            $where['tel'] = ['like' , '%' . $post['tel'] .'%'];
        }
        $info = Db::name('suggestion')->where($where)->order('create_time desc')->limit($limit1)->select();
        $count = Db::name('suggestion')->where($where)->count();
        if($info)
        {
            foreach ($info as $key => $value)
            {
                if($value['supplier_id'])
                {
                    $info[$key]['type'] = '投诉';
                }
                else
                {
                    $info[$key]['type'] = '意见';
                }
            }
            $result = Config::get('code.success');
            $result['data'] = $info;
            $result['count'] = $count;
        }
        else
        {
            $result = Config::get('code.null');
            $result['data'] = [];
            $result['count'] = 0;
        }
        return json($result);
    }

    /**
     * 获取意见反馈详情
     * @return \think\response\Json
     */
    public function getSuggestionInfo()
    {
        $id = input('post.id');
        if($id)
        {
            $info = Db::name('suggestion')->where('id',$id)->find();
            if($info)
            {
                $info['image_info'] = Db::name('image')->whereIn('id',$info['image_ids'])->select();
                if($info['supplier_id'])
                {
                    $info['type'] = '投诉';
                }
                else
                {
                    $info['type'] = '意见';
                }
                $result = Config::get('code.success');
                $result['data'] = $info;
            }
            else
            {
                $result = Config::get('code.error');
            }
        }
        else
        {
            $result = Config::get('code.mistake');
        }
        return json($result);
    }

    /**
     * 常见问题列表数据
     * @return \think\response\Json
     */
    public function getFaqList()
    {
        $post = Request::instance()->post();
        if(array_key_exists('pageNumber' , $post))
        {
            $page = $post['pageNumber'];
        }
        else
        {
            $page = 1;
        }
        $limit = $post['pageSize'] ? $post['pageSize'] : 10;
        $limit1= ($page - 1 ) * $limit . ',' . $limit;
        $where = [];
        if(array_key_exists('name' , $post))
            $where['name'] = ['like' , '%'.$post['name'].'%'];
        $info = Db::name('problem')->where($where)->order('id desc')->limit($limit1)->select();
        $count = Db::name('problem')->where($where)->count();
        if($info)
        {
            foreach ($info as $key => $value)
            {
                $info[$key]['type'] = Db::name('problem_type')->where('id',$value['type_id'])->value('name');
                if($value['status'] == 1)
                {
                    $info[$key]['status'] = '显示';
                }
                else
                {
                    $info[$key]['status'] = '不显示';
                }
            }
            $result = Config::get('code.success');
            $result['data'] = $info;
            $result['count'] = $count;
        }
        else
        {
            $result = Config::get('code.null');
            $result['data'] = [];
            $result['count'] = 0;
        }
        return json($result);
    }



    /**
     * 常见问题详情
     * @return \think\response\Json
     */
    public function getFaqInfo()
    {
        $id = input('post.id');
        if($id)
        {
            $info = Db::name('problem')->where('id',$id)->find();
            if($info)
            {
                $info['type_info'] = Db::name('problem_type')->where('id',$info['type_id'])->value('name');
                $result = Config::get('code.success');
                $result['data'] = $info;
            }
            else
            {
                $result = Config::get('code.null');
            }
        }
        else
        {
            $result = Config::get('code.mistake');
        }
        return json($result);
    }

    /**
     * 获取常见问题类型
     * @return \think\response\Json
     */
    public function getproblemType()
    {
        $info = Db::name('problem_type')->where('status' , 1)->select();
        if($info)
        {
            $result = Config::get('code.success');
            $result['data'] = $info;
        }
        else
        {
            $result = Config::get('code.null');
        }
        return json($result);
    }

    /**
     * 添加常见问题
     * @return \think\response\Json
     */
    public function addProblemInfo()
    {
        $data = Request::instance()->post();
        if($data) {
            $ins_data['name'] = $data['name'];
            $ins_data['name_english'] = $data['name_english'];
            $ins_data['answer'] = $data['answer'];
            $ins_data['status'] = $data['status'];
            $ins_data['type_id'] = $data['type_id'];
            $ins_data['create_time'] = date('Y-m-d H:i:s');
            $status = Db::name('problem')->insert($ins_data);
            if ($status)
            {
                $result = Config::get('code.success');
            }
            else
            {
                $result = Config::get('code.error');
            }
        }
        else
        {
            $result = Config::get('code.mistake');
        }
        return json($result);
    }

    /**
     * 修改常见问题信息
     * @return \think\response\Json
     */
    public function saveProblemInfo()
    {
        $data = Request::instance()->post();
        if(array_key_exists('id' , $data))
        {
            $save_data['name'] = $data['name'];
            $save_data['name_english'] = $data['name_english'];
            $save_data['answer'] = $data['answer'];
            $save_data['status'] = $data['status'];
            $save_data['type_id'] = $data['type_id'];
            Db::name('problem')->where('id',$data['id'])->update($save_data);
            $result = Config::get('code.success');
        }
        else
        {
            $result = Config::get('code.mistake');
        }
        return json($result);
    }

    /**
     * 删除常见问题
     * @return \think\response\Json
     */
    public function delProblem()
    {
        $id = input('post.id');
        if($id)
        {
            $status = Db::name('problem')->where('id',$id)->delete();
            if($status)
            {
                $result = Config::get('code.success');
            }
            else
            {
                $result = Config::get('code.error');
            }
        }
        else
        {
            $result = Config::get('code.mistake');
        }
        return json($result);
    }

    /**
     * 问题分类列表
     * @return \think\response\Json
     */
    public function faqTypeList()
    {
        $data = Request::instance()->post();
        $where = [];
        $page = $data['pageNumber'] ? $data['pageNumber'] : 1;
        $limit = $data['pageSize'] ? $data['pageSize'] : 10;
        $limit1= ($page - 1 ) * $limit . ',' . $limit;
        if(array_key_exists('name' , $data))
            $where['name'] = ['like' , '%'.$data['name'].'%'];
        $info = Db::name('problem_type')->where($where)->limit($limit1)->select();
        $count = Db::name('problem_type')->where($where)->count();
        if($info)
        {
            foreach ($info as $key => $value)
            {
                if($value['status'] == 1)
                {
                    $info[$key]['status'] = '显示';
                }
                else
                {
                    $info[$key]['status'] = '隐藏';
                }
            }
            $result = Config::get('code.success');
            $result['data'] = $info;
            $result['count'] = $count;
        }
        else
        {
            $result = Config::get('code.null');
            $result['data'] = [];
            $result['count'] = 0;
        }
        return json($result);
    }

    /**
     * 分类详情
     * @return \think\response\Json
     */
    public function getFaqTypeInfo()
    {
        $id = input('post.id');
        $info = Db::name('problem_type')->where('id',$id)->find();
        if($info)
        {
            $result = Config::get('code.success');
            $result['data'] = $info;
        }
        else
        {
            $result = Config::get('code.null');
        }
        return json($result);
    }

    /**
     * 添加常见问题分类
     * @return \think\response\Json
     */
    public function addFaqType()
    {
        $data = Request::instance()->post();
        if ($data)
        {
            $ins_data['name'] = $data['name'];
            $ins_data['name_english'] = $data['name_english'];
            $ins_data['status'] = $data['status'];
            $ins_data['create_time'] = date('Y-m-d H:i:s');
            $ins_data['icon'] = $data['icon'];
            $status = Db::name('problem_type')->insert($ins_data);
            if($status)
            {
                $result = Config::get('code.success');
            }
            else
            {
                $result = Config::get('code.error');
            }
        }
        else
        {
            $result = Config::get('code.mistake');
        }
        return json($result);
    }

    /**
     * 上传图片
     * @return \think\response\Json
     */
    public function uploadImg()
    {
        $file = $_FILES;
        if($file)
        {
            $ossClient = new Oss();
            $files     = $ossClient->uploadFile('logo', 'image');
            $img_path = join(",",$files);
            if($img_path)
            {
                $result = Config::get('code.success');
                $result['data'] = ['image' => $img_path];
            }
            else
            {
                $result = Config::get('code.error');
            }
        }
        else
        {
            $result = Config::get('code.mistake');
        }
        return json($result);
    }

    /**
     * 修改问题类型信息
     * @return \think\response\Json
     */
    public function saveFaqTypeInfo()
    {
        $data = Request::instance()->post();
        if(array_key_exists('id' , $data) && !empty($data['id']))
        {
            $save_data['name'] = $data['name'];
            $save_data['name_english'] = $data['name_english'];
            $save_data['status'] = $data['status'];
            $save_data['icon'] = $data['icon'];
            Db::name('problem_type')->where('id',$data['id'])->update($save_data);
            $result = Config::get('code.success');
        }
        else
        {
            if(array_key_exists('name' , $data) && array_key_exists('name_english' , $data) && array_key_exists('status' , $data))
            {
                $ins_data['name'] = $data['name'];
                $ins_data['name_english'] = $data['name_english'];
                $ins_data['status'] = $data['status'];
                $ins_data['create_time'] = date('Y-m-d H:i:s');
                $status = Db::name('problem_type')->insert($ins_data);
                if($status)
                {
                    $result = Config::get('code.success');
                }
                else
                {
                    $result = Config::get('code.error');
                }
            }
            else
            {
                $result = Config::get('code.mistake');
            }
        }
        return json($result);
    }

    /**
     * 删除问题分类
     * @return \think\response\Json
     */
    public function delFaqType()
    {
        $id = input('post.id');
        if($id)
        {
            $status = Db::name('problem_type')->where('id' , $id)->delete();
            if($status)
            {
                $result = Config::get('code.success');
            }
            else
            {
                $result = Config::get('code.error');
            }
        }
        else
        {
            $result = Config::get('code.mistake');
        }
        return json($result);
    }

    /**
     * 获取用户投诉列表
     * @return \think\response\Json
     */
    public function getComplaintList(){
        $post = Request::instance()->post();
        $pageNumber = $post['pageNumber'] ? $post['pageNumber'] : 1;
        $pageSize = $post['pageSize'] ? $post['pageSize'] : 10;
        $where = [];
        if(array_key_exists('tel' , $post['search'])){
            $where['C.tel'] = ['like' , '%' . $post['search']['tel'] .'%'];
        }
        if(array_key_exists('name' , $post['search'])){
            $where['S.name'] = ['like' , '%' . $post['search']['name'] .'%'];
        }
        $info = Db::name('complaint')
            ->alias('C')
            ->join('order O','C.order_id = O.id','left')
            ->join('supplier S','C.supplier_id = S.id','left')
            ->field('O.order_no,C.tel,C.comment,C.add_time,C.id,S.name')
            ->where($where)
            ->order('add_time desc')
            ->paginate($pageSize,false,['page'=>$pageNumber]);
        $list = $info->toArray();
        if(!empty($list['data'])){
            $result = Config::get('code.success');
            $result['data'] = $list['data'];
            $result['count'] = $list['total'];
        }else{
            $result = Config::get('code.null');
            $result['data'] = [];
            $result['count'] = 0;
        }
        return json($result);
    }

    /**
     * 单条用户投诉内容
     * @return \think\response\Json
     */
    public function getComplaintInfo(){
        $post = Request::instance()->post();
        if (!array_key_exists('id' , $post) || empty($post['id'])){
            return json(Config::get('code.miss'));
        }
        $res = Db::name('complaint')
            ->alias('C')
            ->join('supplier S','C.supplier_id = S.id','left')
            ->field('C.comment,C.tel,S.name,C.image_ids,S.manager_tel')
            ->where(['C.id'=>$post['id']])
            ->find();
        $img = explode(',',$res['image_ids']);
        $image = [];
        foreach ($img as $k=>$v){
            $data = Db::name('image')
                ->where(['id'=>$v])
                ->field('image_url')
                ->find();
            $image[$k] = $data;
        }
        if ($res){
            $result = Config::get('code.success');
            $result['data'] = $res;
            $result['data']['image'] = $image;
        }else{
            $result = Config::get('code.error');
        }
        return json($result);
    }
}